home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: "Best fit" algorithm (help)
- Date: 26 Mar 1996 07:21:09 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4j9215INNgbo@keats.ugrad.cs.ubc.ca>
- References: <APC&7'0'22b6b83'874@peg.apc.org>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <APC&7'0'22b6b83'874@peg.apc.org>, <riox@peg.apc.org> wrote:
- >Hello,
- >Can anyone help me?
- >I am looking for a "best fit" algorithm.
- >The problem I have is very similar to finding the best way of fitting the
- >maximum number of songs on (say) 45 minute tape.
-
- This is loosely related to the ``knapsack problem''. The knapsack problem is
- one of choosing numbers from a given set such that they add up to a particular
- number. This is an NP-complete problem to solve in general, but easy for
- special cases. Some encryption techniques known as ``knapsack ciphers'' rely on
- the difficulty of solving the knapsack problem.
-
- Your problem is different, because the selected set of songs don't have to
- cover the tape precisely.
-
- >If one knows the length of each song and the number of songs, how can one
- >find the best arrangement to occupy maximum amount of tape without "cutting"
- >off any songs. Ie, how to minimise the empty space left on the tape.
- >
- >Can you help?
-
- Assuming that you don't care _which_ songs go on the tape, try a ``greedy''
- approach. Always add to the tape a song which will leave the most free space.
- This means starting with the smallest one, and adding the next bigger one and
- so on. This should yield an optimal solution for the constraint of maximizing
- the number of songs that go on the tape. It won't minimize the left-over space,
- however.
-
- The goals of maximizing the number of songs and minimizing left over space are
- quite divergent. Once could minimize the space with just a few songs, or by
- fitting the provable maximum number of songs possible one could end up with a
- large waste. For example, if you have 37 songs that are 1 minute each, and one
- song that is ten minutes long, the greatest number you can fit is 37, but you
- waste 8 minutes of the tape. You can take two songs out and put in the ten
- minute song to end up with no waste, but only 36 songs.
-
- To solve the problem, you have to define a heuristic function: a way of
- determining whether a solution is good, or whether one solution is better than
- another. You can then search the large tree of possibilities until you find
- something that is good enough.
-
- In any case, these sorts of problems are typically covered in books about
- algorithm analysis. No hastily produced Usenet posting can supplant the insight
- offered by a well researched paper or textbook.
- --
-
-